// stores/tagsView.js
import { defineStore } from "pinia";

export const useTagsViewStore = defineStore("tagsView", {
  state: () => ({
    visitedViews: [],
    cachedViews: [],
  }),
  actions: {
    addVisitedViews(view) {
      if (this.visitedViews.some((v) => v.path === view.path)) return;
      this.visitedViews.push({ ...view, title: view.meta.title || "no-name" });
      if (!view.meta.noCache) {
        this.cachedViews.push(view.name);
      }
    },
    delVisitedViews(view) {
      const index = this.visitedViews.findIndex((v) => v.path === view.path);
      if (index !== -1) {
        this.visitedViews.splice(index, 1);
      }
      const cacheIndex = this.cachedViews.indexOf(view.name);
      if (cacheIndex !== -1) {
        this.cachedViews.splice(cacheIndex, 1);
      }
    },
    delOthersViews(view) {
      const index = this.visitedViews.findIndex((v) => v.path === view.path);
      if (index !== -1) {
        this.visitedViews = this.visitedViews.slice(index, index + 1);
      }
      const cacheIndex = this.cachedViews.indexOf(view.name);
      if (cacheIndex !== -1) {
        this.cachedViews = this.cachedViews.slice(cacheIndex, cacheIndex + 1);
      }
    },
    delAllViews() {
      this.visitedViews = [];
      this.cachedViews = [];
    },
  },
});
